package com.gzwanchuang.lszg.view.recyclerexpand;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.gzwanchuang.lszg.R;
import com.gzwanchuang.lszg.proto.Mystock;
import com.gzwanchuang.lszg.view.recyclerexpand.expand.ExpandGroupItemEntity;
import com.gzwanchuang.lszg.view.recyclerexpand.expand.RecyclerExpandBaseAdapter;

public class PatrolGroupAdapter extends RecyclerExpandBaseAdapter<TitlePatrolItem, Mystock.myMachineToolsList.List, RecyclerView.ViewHolder> {


    /**
     * 悬浮标题栏被点击的时候，展开收起切换功能
     */
    public void switchExpand(int adapterPosition) {
        int groupIndex = mIndexMap.get(adapterPosition).getGroupIndex();
        ExpandGroupItemEntity entity = mDataList.get(groupIndex);
        entity.setExpand(!entity.isExpand());
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_ITEM_TIME) {
            TitleItemHolder holder = new TitleItemHolder(
                    LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand_order_title, parent, false));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ExpandGroupItemEntity entity = (ExpandGroupItemEntity) v.getTag();
                    entity.setExpand(!entity.isExpand());
                    notifyDataSetChanged();
                }
            });
            return holder;
        } else {
            return new SubItemHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand_order_sub, parent, false));
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreatePinnedViewHolder(ViewGroup parent, int viewType) {
        TitleItemHolder holder = (TitleItemHolder) super.onCreatePinnedViewHolder(parent, viewType);
        holder.viewSpace.setVisibility(View.GONE);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == VIEW_TYPE_ITEM_TIME) {
            int groupIndex = mIndexMap.get(position).getGroupIndex();
            TitleItemHolder itemHolder = (TitleItemHolder) holder;
            itemHolder.itemView.setTag(mDataList.get(groupIndex));
            String snString = mDataList.get(groupIndex).getParent().getSn_number();
            itemHolder.vHeng.setVisibility(snString.contains(",") ? View.VISIBLE : View.GONE);
            String tempString = snString.replace(",", "\n");
            itemHolder.tvTitleSn.setText(tempString);
            itemHolder.tvNumber.setText(mDataList.get(groupIndex).getParent().getMachines_number());
            itemHolder.imageExpandFlag.setImageResource(
                    mDataList.get(groupIndex).isExpand() ? R.mipmap.ic_up_indicate : R.mipmap.ic_down_indicate);
            if (mDataList.get(groupIndex).isExpand()) {
                if (mIndexMap.get(position).getChildCount() == 0) {
                    itemHolder.viewSpace.setVisibility(View.VISIBLE);
                } else {
                    itemHolder.viewSpace.setVisibility(View.GONE);
                }
            } else {
                itemHolder.viewSpace.setVisibility(View.VISIBLE);
            }
            if (position % 2 == 0) {
                itemHolder.rvRoot.setBackgroundResource(R.drawable.action_one_bg);
            } else {
                itemHolder.rvRoot.setBackgroundResource(R.drawable.action_two_bg);
            }
        } else {
            SubItemHolder subHolder = (SubItemHolder) holder;
            int groupIndex = mIndexMap.get(position).getGroupIndex();
            int childIndex = mIndexMap.get(position).getChildIndex();
            Mystock.myMachineToolsList.List subItem = mDataList.get(groupIndex).getChildList().get(childIndex);
            subHolder.itemView.setTag(subItem);
            subHolder.mTextSn.setText(subItem.getSnCode());
            subHolder.mTextState.setText(subItem.getGoodsCountNum());
        }

    }

    @Override
    public void onBindPinnedViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindPinnedViewHolder(holder, position);
        TitleItemHolder itemHolder = (TitleItemHolder) holder;
        itemHolder.viewSpace.setVisibility(View.GONE);
    }


    static class TitleItemHolder extends RecyclerView.ViewHolder {

        TextView tvTitleSn;
        TextView tvNumber;
        ImageView imageExpandFlag;
        RelativeLayout rvRoot;
        View viewSpace;
        View vHeng;

        TitleItemHolder(View itemView) {
            super(itemView);
            tvTitleSn = itemView.findViewById(R.id.tv_title_sn);
            tvNumber = itemView.findViewById(R.id.tv_number);
            imageExpandFlag = itemView.findViewById(R.id.image_expand_flag);
            rvRoot = itemView.findViewById(R.id.rv_root);
            viewSpace = itemView.findViewById(R.id.view_space);
            vHeng = itemView.findViewById(R.id.v_heng);
        }
    }

    static class SubItemHolder extends RecyclerView.ViewHolder {

        TextView mTextSn;
        TextView mTextState;

        SubItemHolder(View itemView) {
            super(itemView);
            mTextSn = itemView.findViewById(R.id.tv_xi_hao);
            mTextState = itemView.findViewById(R.id.tv_tian_shu);
        }
    }
}
